package com.lv.service.dialog

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lv.common.imp.OnPositionTListener
import com.lv.common.imp.OnPositionTMore
import com.lv.common.utils.showToast
import com.lv.service.R
import com.lv.service.adapter.RecyclerSelectedTag1Adapter
import com.lv.service.adapter.TextTabSelectedMoreAdapter
import com.lv.service.bean.CommonSelectedBean
import com.lxj.xpopup.core.BottomPopupView

/**
 * 作者： ygx
 * 创建日期：2022/10/10 10:32 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：底部公共list选择器 下一步组合弹窗
 */
class RecyclerSelectedTagNextPopupView(
    private val mContext: Context,
    private val mFristBeans: List<CommonSelectedBean>? = null,
//    private val mPersonBeans: List<CommonSelectedBean>? = null,
//    private val mSecondBeans: List<CommonSelectedBean>? = null,
    private val mTitle: String,
    private var mType: Int = -1,//第二个窗类型 0 单列表 团队 1 三列表 范围
    private val mPositionTListener: OnPositionTMore<List<CommonSelectedBean>>? = null,
) :
    BottomPopupView(mContext) {
    //    private val mTitle2: String = ""
//    private var currentFrist: Int = -1//默认没有选中
    private var currentTeam: Int = -1//默认没有选中
    private var mShowNext = false//是否在第一级弹窗中点击了条目
    private var mNextClick = false//是否在第二级弹窗中点击了条目
    private val name = ""
    private var rvContent: RecyclerView? = null
    private var rvNext: RecyclerView? = null
    private var rvPerson: RecyclerView? = null
    private var tvTitle: TextView? = null
    private var rlLoading: RelativeLayout? = null
    private var viewTop: View? = null
    private var viewShadow: TextView? = null
    private val mBeans = mutableListOf<CommonSelectedBean>()
    private val mNextTeamBeans = mutableListOf<CommonSelectedBean>()
    private val mNextAreaBeans = mutableListOf<CommonSelectedBean>()

    private var mAdapter: RecyclerSelectedTag1Adapter? = null//成员 经理
    private var mNextTeamAdapter: RecyclerSelectedTag1Adapter? = null //团队
    private var mNextAreaAdapter: TextTabSelectedMoreAdapter? = null//负责范围
    override fun getImplLayoutId(): Int {
        return R.layout.recycler_selected_tag_next_popup
    }

    override fun onCreate() {
        super.onCreate()
        rvContent = findViewById<RecyclerView>(R.id.rv_content)
        rvNext = findViewById<RecyclerView>(R.id.rv_next)
        rvPerson = findViewById<RecyclerView>(R.id.rv_person)
        rlLoading = findViewById<RelativeLayout>(R.id.rl_loading)
        tvTitle = findViewById<TextView>(R.id.tv_title)
        viewTop = findViewById<View>(R.id.view_top)
        viewShadow = findViewById<TextView>(R.id.tv_submit)
        findViewById<ImageView>(R.id.iv_close).setOnClickListener { dismiss() }

        initView()
        initEvnet()
    }

    private fun initView() {

        tvTitle?.text = mTitle

        val mLinearLayoutManager =
            LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false)
        rvContent?.layoutManager = mLinearLayoutManager
        mAdapter = RecyclerSelectedTag1Adapter(mBeans)
        rvContent?.adapter = mAdapter
        setShowData()
        mAdapter?.onViewItemClickListener = OnPositionTListener { position, data ->
            if (data.tag == "1") {
                return@OnPositionTListener
            }
//            if (!mShowNext) {
            if (mType == position) {
                return@OnPositionTListener
            }
            if (mType != -1) {
                mBeans[mType].selected = false
                mAdapter?.notifyItemChanged(mType)
            }
            mType = position
            mBeans[mType].selected = true
            mAdapter?.notifyItemChanged(mType)
//            }
//            mPositionTListener?.onClick(currentFrist, mBeans[currentFrist])
//            dismiss()
        }

        mNextTeamAdapter = RecyclerSelectedTag1Adapter(mNextTeamBeans)
        rvPerson?.adapter = mNextTeamAdapter
        mNextTeamAdapter?.onViewItemClickListener =
            OnPositionTListener { position, data ->
                mNextClick = true
                if (currentTeam == position) {
                    return@OnPositionTListener
                }
                if (currentTeam != -1) {
                    mNextTeamBeans[currentTeam].selected = false
                    mNextTeamAdapter?.notifyItemChanged(currentTeam)
                }
                currentTeam = position
                mNextTeamBeans[currentTeam].selected = true
                mNextTeamAdapter?.notifyItemChanged(currentTeam)

            }

        val mNextLayoutManager = GridLayoutManager(context, 3)
        rvNext?.layoutManager = mNextLayoutManager
        mNextAreaAdapter = TextTabSelectedMoreAdapter(mNextAreaBeans)
        rvNext?.adapter = mNextAreaAdapter
        mNextAreaAdapter?.onViewClickListener = OnPositionTListener { position, data ->
            mNextClick = true
            mNextAreaBeans[position].selected = !mNextAreaBeans[position].selected
            mNextAreaAdapter?.notifyItemChanged(position)

        }


    }

    private fun initEvnet() {

        viewTop?.setOnClickListener {

            dismiss()
        }

        viewShadow?.setOnClickListener {
//            mPositionTListener?.onClick(current, mBeans[current])
            if (!mShowNext) {
                if (mType == -1) {
                    showToast("请至少选择一项")
                    return@setOnClickListener
                }
                viewShadow?.text = "确定"
                mShowNext = true
                rvContent?.visibility = View.GONE

                if (mType == 1) {//负责范围
                    rlLoading?.visibility = View.VISIBLE

//                    rvContent?.visibility = View.GONE
//                    rvPerson?.visibility = View.GONE
//                    rvNext?.visibility = View.VISIBLE
                    tvTitle?.text = "负责范围"
                    mPositionTListener?.onClick(-1, mBeans, mNextTeamBeans, mNextAreaBeans)

                } else {
                    rlLoading?.visibility = View.VISIBLE
                    mPositionTListener?.onClick(-2, mBeans, mNextTeamBeans, mNextAreaBeans)

//                    rvPerson?.visibility = View.VISIBLE
//                    rvContent?.visibility = View.GONE
//                    rvNext?.visibility = View.GONE
//                    mPersonBeans?.let {
//                        mNextTeamBeans.clear()
//                        for (i in mPersonBeans.indices) {
//                            val commonSelectedBean = mPersonBeans[i]
//                            if (mType == 1) {
//                                commonSelectedBean.selected = false
//                            } else {
//                                if (commonSelectedBean.selected) {
//                                    currentTeam = i
//                                }
//                            }
//                            mNextTeamBeans.add(commonSelectedBean)
//                        }
//                    }

//                    mNextTeamAdapter = RecyclerSelectedTag1Adapter(mNextTeamBeans)
//                    rvPerson?.adapter = mNextTeamAdapter
//                    mNextTeamAdapter?.onViewItemClickListener =
//                        OnPositionTListener { position, data ->
//
//                            if (currentTeam == position) {
//                                return@OnPositionTListener
//                            }
//                            if (currentTeam != -1) {
//                                mNextTeamBeans[currentTeam].selected = false
//                                mNextTeamAdapter?.notifyItemChanged(currentTeam)
//                            }
//                            currentTeam = position
//                            mNextTeamBeans[currentTeam].selected = true
//                            mNextTeamAdapter?.notifyItemChanged(currentTeam)
//
//                        }
                    tvTitle?.text = "所属团队"

                }
            } else {
                if (mNextClick) {
                    mPositionTListener?.onClick(mType, mBeans, mNextTeamBeans, mNextAreaBeans)
                    dismiss()
                } else {
                    showToast("请选择内容")
                }
            }
        }
    }

    private fun setDismissDialog() {
        dismiss()
    }


    override fun onDestroy() {
        super.onDestroy()
    }

    override fun beforeDismiss() {
        super.beforeDismiss()


    }

    fun setHeader(mPersonBeans: List<CommonSelectedBean>?) {
        mPersonBeans?.let {
            mNextTeamBeans.clear()
            for (i in mPersonBeans.indices) {
                val commonSelectedBean = mPersonBeans[i]
                if (mType == 1) {
                    commonSelectedBean.selected = false
                } else {
                    if (commonSelectedBean.selected) {
                        currentTeam = i
                        mNextClick = true
                    }
                }
                mNextTeamBeans.add(
                    CommonSelectedBean(
                        commonSelectedBean.name,
                        commonSelectedBean.tag,
                        id = commonSelectedBean.id,
                        tag2 = commonSelectedBean.tag2,
                        selected = commonSelectedBean.selected
                    )
                )
            }
        }
        mNextTeamAdapter?.notifyDataSetChanged()
        rvPerson?.visibility = View.VISIBLE
        rvNext?.visibility = View.GONE
        rlLoading?.visibility = View.GONE
    }

    fun setArea(mSecondBeans: List<CommonSelectedBean>?) {
        mSecondBeans?.let {
            mNextAreaBeans.clear()
            for (i in mSecondBeans.indices) {
                val commonSelectedBean = mSecondBeans[i]
                if (mType == 0) {
                    commonSelectedBean.selected = false
                }
                if (commonSelectedBean.selected) {
                    mNextClick = true
                }
                mNextAreaBeans.add(
                    CommonSelectedBean(
                        commonSelectedBean.name,
                        commonSelectedBean.tag,
                        commonSelectedBean.selected,
                        id = commonSelectedBean.id,
                        tag2 = commonSelectedBean.tag2,
                    )
                )
            }
            mNextAreaAdapter?.notifyDataSetChanged()
            rvPerson?.visibility = View.GONE
            rvNext?.visibility = View.VISIBLE
            rlLoading?.visibility = View.GONE
        }
    }


    fun setShowData() {
//        showToast("数据更新")

        mFristBeans?.let {
            mBeans.clear()
            for (i in mFristBeans.indices) {
                val commonSelectedBean = mFristBeans[i]
                if (commonSelectedBean.selected) {
                    mType = i
                }
                mBeans.add(
                    CommonSelectedBean(
                        commonSelectedBean.name, mFristBeans[i].tag, commonSelectedBean.selected
                    )
                )
            }

        }
//        mPersonBeans?.let {
//            mNextTeamBeans.clear()
//            for (i in mPersonBeans.indices) {
//                val commonSelectedBean = mPersonBeans[i]
//                if (mType == 1) {
//                    commonSelectedBean.selected = false
//                } else {
//                    if (commonSelectedBean.selected) {
//                        currentTeam = i
//                    }
//                }
//                mNextTeamBeans.add(
//                    CommonSelectedBean(
//                        commonSelectedBean.name,
//                        commonSelectedBean.tag,
//                        commonSelectedBean.selected
//                    )
//                )
//            }
//        }
//        mSecondBeans?.let {
//            mNextAreaBeans.clear()
//            for (i in mSecondBeans.indices) {
//                val commonSelectedBean = mSecondBeans[i]
//                if (mType == 0) {
//                    commonSelectedBean.selected = false
//                }
//                mNextAreaBeans.add(
//                    CommonSelectedBean(
//                        commonSelectedBean.name,
//                        commonSelectedBean.tag,
//                        commonSelectedBean.selected
//                    )
//                )
//            }
//        }
        mAdapter?.notifyDataSetChanged()
//        mNextTeamAdapter?.notifyDataSetChanged()
//        mNextAreaAdapter?.notifyDataSetChanged()
    }

}